<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Song extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
    }

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		redirect('admin_panel/song/main');
	}
    
    public function main()
    {
        //init
        $crud = new grocery_CRUD();
        $crud->set_table('songs');
        $crud->set_subject('Songs');
        
        //db
        $crud->where(array('songs.deleted' => 0, 'songs.status' => 'active'));
        $crud->order_by('songs.title');
        
        //settings before saving
        $crud->columns('title','filename','genre_id','speed');
        $crud->fields('title','filename','genre_id','speed','remarks','status');
        $crud->display_as('genre_id','Genre');
        
        $crud->set_relation('genre_id','genres','name');
        $crud->set_field_upload('filename', 'uploads/songs');
        
        //additional action buttons
        $crud->add_action('Remove Song',site_url('html/images/soft_delete.png'),'admin_panel/song/delete','confirm-delete');
        //$crud->add_action('Record Playlist',site_url('html/images/playlist.png'),'admin_panel/client/playlist/');
        
        $crud->unset_fields('deleted');
        $crud->unset_delete();
        
        //settings validation before saving
        $crud->set_rules('title','Song title','required');
        $crud->set_rules('genre_id','Genre','required');
        
        $output = $crud->render();
        $this->load->vars($output);
        
        //genre
        $mparam = array(
            'table'         =>  'genres',
            'condition'     =>  array('deleted' => 0),
            'order_by'      =>  'name'
        );
        
        $data['genres'] = $this->general_model->_get_record($mparam);
        
        $this->load->view('admin_panel/song/main',$data);
    }
    
    public function delete($song_id)
    {
        $param = array(
            'table'     =>  'songs',
            'fields'    =>  array('deleted' => 1),
            'condition' =>  array('id' => $song_id)
        );
        if($this->general_model->_save_record($param))
        {
            $this->session->set_flashdata('success_message','Song record removed.');
            redirect('admin_panel/song/main');
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */